define(["jquery", "easy-admin","xm-select"], function ($, ea) {



    let laydate = layui.laydate
        , form = layui.form;
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'point.pointgoods/index',
        add_url: 'point.pointgoods/add',
        edit_url: 'point.pointgoods/edit',
        delete_url: 'point.pointgoods/delete',
        export_url: 'point.pointgoods/export',
        modify_url: 'point.pointgoods/modify',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', width: 60, title: 'id'},
                    {field: 'title', width: 150, title: '商品名称'},
                    {field: 'logo', title: '商品logo', templet: ea.table.image, width: 120,search:false},
                    {field: 'price', title: '销售价', width: 120},
                    {field: 'g_points', title: '所需积分',width: 120},
                    {field: 'sales', title: '销量',width: 120, search: false},
                    {field: 'stock', title: '库存', width: 120,},
                    {field: 'status', title: '状态', templet: ea.table.switch, width: 120, selectList: {0: '下架', 1: '上架'}, tips:"上架|下架"},
                    {field: 'create_time',width: 170, title: '创建时间',search: 'range'},
                    {width: 200, title: '操作', templet: ea.table.tool},
                ]],
            });

            ea.listen();
        },
        add: function () {

            let pay_type = xmSelect.render({
                el: '#pay_type',
                on: function (data) {
                    var arr = data.arr;
                    var paytype=[];
                    if (arr.length > 0) {
                        for (let i = 0; i < arr.length; i++) {
                            paytype.push(arr[i].value);
                            $("#paytype").val(paytype);
                        }
                    }
                },
                radio: false,
                toolbar: {show: true},
                name: 'pay_type',
                paging: true,
                filterable: true,
                tips: '选择支付方式',
                data: [],
            });
            //获取支付方式
            $.get("/admin/system.config/getPaymentByList").then(res => {
                if (res.code == 1) {
                    pay_type.update({
                        data: res.data
                    })
                }
            });
            form.on('radio(selectAddress)', function (data) {
                if (data.value == 0) {
                    $(".setAddress").addClass('layui-hide')
                } else {
                    $(".setAddress").removeClass('layui-hide')
                }
            });
            const province = xmSelect.render({
                el: '#province',
                autoRow: true,
                paging: true,
                filterable: true,
                toolbar: {
                    show: true,
                },
                name: 'address',
                model: {
                    label: {
                        type: 'block',
                        block: {
                            //最大显示数量, 0:不限制
                            showCount: 2,
                            //是否显示删除图标
                            showIcon: true,
                        }
                    }
                },
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    expandedKeys: [-1],
                    lazy: true,
                    icon: 'hidden',

                    load: function (item, cb) {
                        console.log(item)
                        setTimeout(function () {
                            if (item.name.endsWith('2')) {
                                return cb([]);
                            }
                            cb([
                                {name: item.name + 1, value: item.value + '1', children: []},
                                {name: item.name + 2, value: item.value + '2', children: []},
                            ])
                        }, 500)
                    }
                },
                height: '200px',
                data: []
            });
            //获取城市地址
            $.get("/admin/system.province/getProvinceCityByListTree").then(res => {
                if (res.code == 1) {
                    province.update({
                        data: res.data
                    })
                }
            });
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
    };
    return Controller;
});